
//记录查询出的所有用户信息
var replyCustomMenuobj=new Array();

//记录点击当前行的用户ID 修改用户信息使用
var replyCustomMenuGridnwxMessageId;

function replyCustomMenuGridColumnsRenderRolelink(nwxMessageId,sname){
	replyCustomMenuGridnwxMessageId=nwxMessageId;
	showreplyCustomMenuRoleForm(nwxMessageId,sname);
}
function replyCustomMenuGridColumnsRenderdellink(nwxMessageId,sname){
	
	Ext.MessageBox.confirm('信息', '是否确认删除消息： '+sname+'?', function(btn){
		
		if(btn=='yes'){
			Ext.Ajax.request({
				url:'delReplyNotice.do',
				method:'POST',
				params:{
					ids:nwxMessageId
				},
				success:function(response){
					if(response.responseText=='1'){
						Ext.MessageBox.alert('信息','删除成功！');
						replyCustomMenuGridStore.reload();
					}else{
						Ext.MessageBox.alert('ERROR','删除失败！');
					}
				},
				failure:function(){
					Ext.Msg.alert('ERROR','错误');
				}
			});
		}
	});
}

function replyCustomMenuGridColumnsRenderInfolink(nwxMessageId){
	replyCustomMenuGridnwxMessageId=nwxMessageId;
	if(replyCustomMenuGridnwxMessageId==null)return ;
	showreplyCustomMenuAddForm(2,replyCustomMenuobj[nwxMessageId]);
}

var replyCustomMenuAddWin;
var replyCustomMenuTitle=new Ext.form.TextField({
		labelAlign:'right',
		width:390,
		fieldLabel: '消息标题'
});
var replyCustomMenusDescription=new Ext.form.TextArea({
		labelAlign:'right',
		width:390,
		height:100,
		fieldLabel: '消息描叙'
});
var replyCustomMenusPicUrl=new Ext.form.FileUploadField({
		id: 'form-file',
		width:188,
		buttonText: '浏览...',
        emptyText: '选择一张图片',
        fieldLabel: '上传图片',
        name: 'txtpath',
        buttonCfg: {
        	width: 30,
            iconCls: 'upload-icon'
        }
});
var replyCustomMenusUrl=new Ext.form.ComboBox({
		fieldLabel: '点击跳转链接',
		width:155,
        store: new Ext.data.JsonStore({
        	autoLoad:true,
            url:'selectUserUrl.do',
            fields:['sName','nWxUserUrlId']
        }),
        mode:'local',
	    valueField:'nWxUserUrlId',
	    displayField:'sName',
        emptyText:'全部',
        triggerAction:'all',
        selectOnFocus:true
	});
var replyCustomMenunisValid=new Ext.form.Checkbox({
		fieldLabel:'是否有效',
		labelAlign:'right',
		checked:true
});

var replyCustomMenudelPicBtn=new Ext.Button({
		text:'取消上传',
		handler:function(){
			replyCustomMenusPicUrl.reset();
		}
});
var replyCustomMenunOrder=new Ext.form.NumberField({
		labelAlign:'right',
		width:155,
		value:0,
		fieldLabel: '排序号'
});
var replyCustomMenunNkey=new Ext.form.NumberField({
		labelAlign:'right',
		width:155,
		value:0,
		fieldLabel: 'key值'
});

var replyCustomMenuExplain=new Ext.form.Label({
	labelAlign:'right',
	text:'说明：一组相同key值 为一条消息。排序号按数字从小到大排列 多条图文消息信息中，默认第一条消息显示为大图,注意，消息数不能超过10条（上传图片仅支持JPG、PNG格式，较好的效果为大图360*200，小图200*200）'
});

var replyCustomMenuImage = new Ext.BoxComponent({
//    id: 'imgPosition',  
    //hidden:true,  
    xtype: 'box',  
    width: 120,  
    height: 100,  
    style: '',  
    autoEl: {
        tag: 'img',
        id:"imgPosition",
	 	name:"imgPosition",
        src: '' //指定url路径  
    }  
});

//tinfo参数值1 :添加 2:修改
function showreplyCustomMenuAddForm(tinfo,replyCustomMenuJson) {
	
	if(!replyCustomMenuAddWin){
		replyCustomMenuAddform = new Ext.FormPanel({
			fileUpload: true,
		    border: false,
		    labelAlign:'right',
		    frame:true,
		    items: [
		    	{columnWidth:1.0,layout:'column',labelAlign:'right',items:[
		    		{columnWidth:.6,layout:'form',labelAlign:'right',items:[replyCustomMenusPicUrl]},replyCustomMenudelPicBtn
		    	]},
                {layout:'column',items:[
                	 {columnWidth:.65,layout:'form',items:[
                	 replyCustomMenusUrl,replyCustomMenunOrder,replyCustomMenunNkey,replyCustomMenunisValid
	                ]},
	                {columnWidth:.35,layout:'form',items:[
	                	replyCustomMenuImage
	                ]}
                ]},
            	replyCustomMenuTitle,replyCustomMenusDescription,replyCustomMenuExplain
		    ],
		    buttons: [{
		        text: tinfo,
		        handler: function() {
		        	if(this.text=='添 加'){
		        		 replyCustomMenuAddform.getForm().submit({
		                    url: 'saveReplyCustomMenu.do',
		                    method:'POST',
		                    waitMsg: 'Uploading your data...',
		                    params : {
		                    	sTitle:replyCustomMenuTitle.getValue(),
								sDescription:replyCustomMenusDescription.getValue(),
			                   	nWxUserUrlId:replyCustomMenusUrl.getValue(),
			                   	nOrder:replyCustomMenunOrder.getValue(),
			                   	nkey:replyCustomMenunNkey.getValue(),
			                   	nisValid:replyCustomMenunisValid.checked?1:2
		                    },
		                    success: function(fm, o){
		                    	if(o.result.success=='1'){
		                    		Ext.MessageBox.alert('信息!', '添加成功!');
			                        replyCustomMenuAddform.getForm().reset();
			                        replyCustomMenuAddWin.hide();
			                        replyCustomMenuGridStore.reload();
		                    	}else if(o.result.success=='3'){
		                    		Ext.MessageBox.alert('Error!', '图片格式不正确（仅支持JPG、PNG）!');
		                    	}else{
		                    		Ext.MessageBox.alert('Error!', '添加失败!');
		                    	}
		                    },
							failure:function(form, action) {
								Ext.Msg.alert('错误', '添加失败！');
								return ;
							}
		                });
		        	}else if(this.text=='修 改'){
		        		replyCustomMenuAddform.getForm().submit({
		                    url: 'updateReplyNotice.do',
		                    method:'POST',
		                    waitMsg: 'Uploading your data...',
		                    params : {
		                    	sTitle:replyCustomMenuTitle.getValue(),
								sDescription:replyCustomMenusDescription.getValue(),
			                   	nWxUserUrlId:replyCustomMenusUrl.getValue(),
			                   	nOrder:replyCustomMenunOrder.getValue(),
			                   	nkey:replyCustomMenunNkey.getValue(),
			                   	nisValid:replyCustomMenunisValid.checked?1:2,
			                   	spicUrl:replyCustomMenusPicUrl.getValue(),
			                   	nwxMessageId:replyCustomMenuGridnwxMessageId
		                    },
		                    success: function(fm, o){
		                    	if(o.result.success=='1'){
		                    		Ext.MessageBox.alert('信息!', '修改成功!');
			                        replyCustomMenuAddform.getForm().reset();
			                        replyCustomMenuAddWin.hide();
			                        replyCustomMenuGridStore.reload();
		                    	}else if(o.result.success=='3'){
		                    		Ext.MessageBox.alert('Error!', '图片格式不正确（仅支持JPG、PNG）!');
		                    	}else{
		                    		Ext.MessageBox.alert('Error!', '修改失败!');
		                    	}
		                    },
							failure:function(form, action) {
								Ext.Msg.alert('错误', '修改失败！');
								return ;
							}
		                });
		        	}
		        }
		    },{
		        text: '返 回',
		        handler: function() {
		        	replyCustomMenuAddform.getForm().reset();
		        	replyCustomMenuAddWin.hide();
		        }
		    }]
		});
		
		replyCustomMenuAddWin =new Ext.Window({
		    title: '回复信息',
		    closeAction: 'hide',
		    width: 600,
		    height: 390,
		    layout: 'fit',
	        resizable: true,
	        modal: true,
	        items: replyCustomMenuAddform,
	        listeners :{
	        	close:function(){
	        		replyCustomMenuAddform.getForm().reset();
	        	}
	    	}
		});
	}
    replyCustomMenuAddWin.show();
    
    if(tinfo==2){
    	replyCustomMenuAddWin.setTitle('信息：'+replyCustomMenuJson.stitle);
    	replyCustomMenuAddform.buttons[0].setText('修 改');
    	
    	if(replyCustomMenuJson.nkey!=null)
    		replyCustomMenunNkey.setValue(replyCustomMenuJson.nkey);
    	if(replyCustomMenuJson.norder!=null)
    		replyCustomMenunOrder.setValue(replyCustomMenuJson.norder);
    	if(replyCustomMenuJson.nisValid!=null)
    		replyCustomMenunisValid.setValue(replyCustomMenuJson.nisValid==1?true:false);
    	if(replyCustomMenuJson.stitle!=null)
    		replyCustomMenuTitle.setValue(replyCustomMenuJson.stitle);
    	if(replyCustomMenuJson.sdescription!=null)
    		replyCustomMenusDescription.setValue(replyCustomMenuJson.sdescription);
    	if(replyCustomMenuJson.nwxUserUrlId!=null)
    		replyCustomMenusUrl.setValue(replyCustomMenuJson.nwxUserUrlId);
    	if(replyCustomMenuJson.spicUrl!=null){
    		replyCustomMenusPicUrl.setValue('1');
    		Ext.getDom('imgPosition').src=replyCustomMenuJson.spicUrl==''?'#':replyCustomMenuJson.spicUrl;
    	}
	}else{
		replyCustomMenuAddform.buttons[0].setText('添 加');
		replyCustomMenuAddform.getForm().reset();
		Ext.getDom('imgPosition').src='#';
	}
}

Ext.onReady(function(){
	replyCustomMenuiswriteidb= Ext.get('replyCustomMenuiswriteid').getValue()==1?true:false;
	
	var replyCustomMenunkey=new Ext.form.ComboBox({
		fieldLabel: 'key值',
        store: new Ext.data.JsonStore({
        	autoLoad:true,
            url:'selectUserKeys.do',
            fields:['nKey','nKey']
        }),
        mode:'local',
	    valueField:'nKey',
	    displayField:'nKey',
        emptyText:'全部',
        triggerAction:'all',
        selectOnFocus:true
	});
	
	var replyCustomMenuSearchBtn=new Ext.Button({
		text:'查询',
		handler:function(){
			replyCustomMenuGridStore.load({
				params : {
					start : 0,
					limit : 20,
					'nkey':replyCustomMenunkey.getValue()
				}
			});
		}
	});
	
	var replyCustomMenuSearch=new Ext.Panel({
		height:50,
		region:'north',
		layout:'column',
		frame: true,
		border:false,
		bodyBorder:false,
		items:[
			{columnWidth:.38,layout:'form',labelAlign:'right',items:[replyCustomMenunkey]},
			{columnWidth:.2,layout:'form',labelAlign:'right',items:[replyCustomMenuSearchBtn]}
		]
	});
	
	replyCustomMenuGridStore = new Ext.data.Store({
        autoLoad: true,
        proxy : new Ext.data.HttpProxy({
            url: 'searchReplyCustomMenu.do'
        }),
        baseParams:{
        	'nkey':replyCustomMenunkey.getValue()
        },
        reader:new Ext.data.JsonReader({
             totalProperty: 'totalSize',
             root:'list'
        },[
        	{name: 'norder',mapping:'norder', type: 'int'},
            {name: 'nuserId',mapping:'nuserId', type: 'int'},
        	{name: 'nisValid',mapping:'nisValid',   type: 'int'},
        	{name: 'nwxMessageId',mapping:'nwxMessageId', type: 'int'},
        	{name: 'stitle',mapping:'stitle', type: 'string'},
        	{name: 'sdescription',mapping:'sdescription', type: 'string'},
        	{name: 'spicUrl',mapping:'spicUrl', type: 'string'},
        	{name: 'nwxUserUrlId',mapping:'nwxUserUrlId', type: 'int'},
        	{name: 'nkey',mapping:'nkey', type: 'int'},
        	{name: 'dcreateTime',mapping:'dcreateTime', type: 'date',
        		convert : function(v) {
        			return new Date(v);
        		}
        	}
        ])
    });
    
    replyCustomMenuGridStore.on("beforeload", function(thiz, options) {
		thiz.baseParams["nkey"] = replyCustomMenunkey.getValue();
	});
    
    replyCustomMenuAddBtn =new Ext.Button({
		text:'添加消息',
		hidden:replyCustomMenuiswriteidb,
		handler:function(){
			showreplyCustomMenuAddForm(1,'');
		}
	});
    
    var replyCustomMenuGridBar=new Ext.PagingToolbar({
		store: replyCustomMenuGridStore,
		displayInfo: true,
		pageSize:20,
		displayMsg : '显示第 {0} 条到 {1} 条记录，一共 {2} 条',
		emptyMsg: "没有记录可以显示",
		beforePageText:'当前页码',
		afterPageText:'共{0}页', 
		firstText:'第一页', 
		prevText:'上一页', 
		nextText:'下一页', 
		lastText:'最后一页', 
		refreshText:'刷新当前页',
		items:['-', replyCustomMenuAddBtn]
     });
     
    replyCustomMenuGridColumnsRenderInfo=function(value, cellmeta, record, rowIndex, columnIndex, store){
    	replyCustomMenuobj[record.data.nwxMessageId]=record.json;
    	var linkhtml='';
    	if(replyCustomMenuiswriteidb==false){
	    	linkhtml='<a style="margin-right:20px" onclick="replyCustomMenuGridColumnsRenderInfolink('+record.data.nwxMessageId+')" href="javascript:void(0)">详细 </a>';
	    	linkhtml+='<a onclick="replyCustomMenuGridColumnsRenderdellink('+record.data.nwxMessageId+',\''+record.data.stitle+'\')" href="javascript:void(0)"> 删除</a>';
    	}
    	return linkhtml;
	};
	
	replyCustomMenuGrid = new Ext.grid.GridPanel({
        store: replyCustomMenuGridStore,
        region:'center',
		disableSelection: true,
		border:false,
		frame:true,
		stripeRows:true,
		loadMask :{msg:"数据加载中，请稍等..."},
		layout:'fit',
		viewConfig:{
			forceFit: true,
			scrollOffset:0
		},
        cm:new Ext.grid.ColumnModel([
        	new Ext.grid.RowNumberer(),
            {header: "排序号", width:30, dataIndex: 'norder', sortable: true},
            {header: "标题", flex: 1, dataIndex: 'stitle', sortable: true},
            {header: "描叙",flex: 1, dataIndex: 'sdescription', sortable: true},
            {header: "key值", width:30, dataIndex: 'nkey', sortable: true},
            {header: "是否有效", width:50,sortable: true,dataIndex: 'nisValid',
            	renderer:function(value){
            		if (value == 1) {
			            return '有效';
			        }else{
			        	return '无效';
			        }
            	}
            },
            {header: "创建时间",flex: 1, dataIndex: 'dcreateTime', sortable: true,renderer : Ext.util.Format.dateRenderer('Y-m-d H:i:s')},
            {header: "操作", width:150,align:'center',sortable: true,renderer:replyCustomMenuGridColumnsRenderInfo}
        ]),
        bbar: replyCustomMenuGridBar
    });
    
	 var mainPanel = new Ext.Viewport({
	 	layout:'border',
		renderTo:'replyCustomMenuDIV',
		id:'replyCustomMenuMainPanel',
		items:[replyCustomMenuGrid,replyCustomMenuSearch],
		listeners: {
	        'resize': function(viewport,adjWidth,adjHeight,rawWidth,rawHeight)  {
	          	Ext.getCmp("replyCustomMenuMainPanel").setHeight((document.body.clientHeight - 290) * 0.67);  
			}
    	}
	});
})